package com.pagepub.core.tag.validator;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

public class ValidateCardNo extends FormValidateBasic {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3518394468451063145L;

	private String rightimage;

	private String rightwordcolor;

	private String submitvalidate;

	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();

		try {
			out.print(validatecardno());
		} catch (IOException e) {
			// TODO 自动生成 catch 块

			e.printStackTrace();
		}
		// TODO 自动生成方法存根
		return super.doStartTag();
	}

	public StringBuffer validatecardno() {
		StringBuffer str = new StringBuffer();
		String emptyelement = "";
		String pluselement = "";
		if (this.submitvalidate.equalsIgnoreCase("true")) {
			emptyelement = "emptyelement(realname);";
			pluselement = " pluselement(realname);";
		}
		str.append("<script language=\"javascript\">");
		str.append("function validatecardno(realvalue,spanname,realname)");
		str.append("{  if(realvalue!=\"\"){");
		str.append("    if (isNaN(realvalue)&&realvalue.length!=18) ");
		str.append("    {  ");
		str.append("	message=\"对不起,您输入的不是数字\";");
		str.append(" imgurl=\"<img src=\\\"" + this.getWrongimage()
				+ "\\\"/>\";");
		str.append(this.display());
		str.append("  return false;");
		str.append("    }");
		str.append("    var len = realvalue.length, re;");
		str.append("     if (len == 15)");
		str
				.append("      re = new RegExp(/^(d{6})()?(d{2})(d{2})(d{2})(d{3})$/);");
		str.append("   else if (len == 18)");
		str
				.append("re = new RegExp(/^(d{6})()?(d{4})(d{2})(d{2})(d{3})(d)$/);");
		str.append("    else ");
		str.append("    {");
		str.append("	message=\"对不起,您输入的身份证号位数不对!\";");
		str.append(" imgurl=\"<img src=\\\"" + this.getWrongimage()
				+ "\\\"/>\";");
		str.append(this.display());
		str.append(pluselement);
		str.append("   return false");
		str.append("  }");
		str.append("  var a = realvalue.match(re);");
		str.append("   if (a != null)");
		str.append("   {");
		str.append("    if (len==15)");
		str.append("     {");
		str
				.append("      var D = new Date(\"19\"+a[3]+\"/\"+a[4]+\"/\"+a[5]);");
		str
				.append("       var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];");
		str.append("    }");
		str.append("    else");
		str.append("    {");
		str.append("      var D = new Date(a[3]+\"/\"+a[4]+\"/\"+a[5]);");
		str
				.append("      var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];");
		str.append("     }");
		str.append("     if (!B) ");
		str.append("    {");
		str.append("	message=\"对不起,您输入的身份证号里出生日期不对!\";");
		str.append(" imgurl=\"<img src=\\\"" + this.getWrongimage()
				+ "\\\"/>\";");
		str.append(this.display());
		str.append(pluselement);
		str.append("     return false;");
		str.append("   }");
		str.append("  }");
		str.append("	message=\"恭喜！您输入的身份证号可以使用!\";");
		str.append(" imgurl=\"<img src=\\\"" + this.getRightimage()
				+ "\\\"/>\";");
		str
				.append("document.getElementById(spanname).innerHTML=imgurl+\"<font color=\\\""
						+ this.rightwordcolor + "\\\">\"+message+\"</font>\";");
		str.append(emptyelement);
		str.append("   return true;");
		str.append("} }");
		str.append("</script>");
		return str;

	}

	public String getRightimage() {
		return rightimage;
	}

	public void setRightimage(String rightimage) {
		this.rightimage = rightimage;
	}

	public String getRightwordcolor() {
		return rightwordcolor;
	}

	public void setRightwordcolor(String rightwordcolor) {
		this.rightwordcolor = rightwordcolor;
	}

	public String getSubmitvalidate() {
		return submitvalidate;
	}

	public void setSubmitvalidate(String submitvalidate) {
		this.submitvalidate = submitvalidate;
	}
}
